package com.gitee.wsl.ext.string

import kotlin.jvm.JvmName


fun String.toBooleanSmart(): Boolean {
    return toBooleanSmartOrNull()?: throw IllegalArgumentException("Cannot convert '$this' to boolean")
}

fun String.toBooleanSmartOrNull(): Boolean? {
    return when (this) {
        "true", "1", "yes", "on" -> true
        "false", "0", "no", "off" -> false
        else -> null
    }
}


/**
 * Returns 'true' when the string starts with any of the given prefixes
 */
fun String.startsWithAny(vararg prefixes: String) = startsWithAny(prefixes)

/**
 * Returns 'true' when the string starts with any of the given prefixes
 */
@JvmName("startsWithAnyArray")
fun String.startsWithAny(prefixes: Array<out String>) = prefixes.any { startsWith(it) }

/**
 * Returns 'true' when the string starts with any of the given prefixes
 */
@JvmName("startsWithAnyCollection")
fun String.startsWithAny(prefixes: Collection<String>) = prefixes.any { startsWith(it) }

/**
 * Returns 'true' when the string does NOT start with any of the given prefixes
 */
fun String.startsWithNone(vararg prefixes: String) = !startsWithAny(prefixes)

/**
 * Returns 'true' when the string does NOT start with any of the given prefixes
 */
@JvmName("startsWithNoneArray")
fun String.startsWithNone(prefixes: Array<out String>) = !startsWithAny(prefixes)

/**
 * Returns 'true' when the string does NOT start with any of the given prefixes
 */
@JvmName("startsWithNoneCollection")
fun String.startsWithNone(prefixes: Collection<String>) = !startsWithAny(prefixes)
